<?php

/*
 * Copyright 2008 the original author or authors.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#namespace tehframework\objects\propertyeditors;

require_once 'test/testHelper.php';

class IntegerEditorTest extends BaseEditorTestCase
{
	/**
	 * @var IntegerEditor
	 */
	protected $e;
	
	protected function createEditor()
	{
		return new IntegerEditor();
	}
	
	function testConstructSetsCountObjectsProperty()
	{
		$e = new IntegerEditor(false);
		$o = new ArrayObject(array(1, 2, 3));
		
		$this->assertSame(0, $e->editProperty($o));
	}
	
	function testConstructSetsCountArraysProperty()
	{
		$e = new IntegerEditor(null, true);
		$a = array(1, 2, 3);
		
		$this->assertSame(count($a), $e->editProperty($a));
	}
	
	function testCanEditPropertyReturnsTrueForAnyType()
	{
		$h = fopen(__FILE__, 'r');
		
		try
		{
			$this->assertTrue($this->e->canEditProperty($h));
		}
		catch (Exception $x)
		{
			fclose($h);
			
			throw $x;
		}
		
		$propertyValues = array(
			1, 1.1, 'foo', new stdClass, $this, true, false, null, array(1, 'two'));
		
		foreach ($propertyValues as $propertyValue)
		{
			$this->assertTrue($this->e->canEditProperty($propertyValue));
		}
	}
	
	function testEditPropertyDoesNotCountArraysByDefault()
	{
		$this->assertSame(0, $this->e->editProperty(array(1, 2, 3)));
	}
	
	function testEditPropertyDoCountObjectsByDefault()
	{
		$o = new ArrayObject(array(1, 2, 3));
		
		$this->assertSame(count($o), $this->e->editProperty($o));
	}
	
	function testEditPropertyReturnsSpecifiedValueIfItsTypeIsInteger()
	{
		$this->assertSame(666, $this->e->editProperty(666));
	}
	
	function testEditPropertyReturnsZeroIfCountObjectsIsFalse()
	{
		$this->assertSame(0,
			$this->e->setCountObjects(false)->editProperty(new stdClass));
	}
	
	function testEditPropertyReturnsZeroIfCountObjectsIsFalseAndObjectImplementsCountable()
	{
		$this->assertSame(0, $this->e
			->setCountObjects(false)->editProperty(new ArrayObject(array(1, 2, 3))));
	}
	
	function testEditPropertyReturnsResultOfCountIfCountObjectsIsTrueAndSpecifiedValueIsCountableObject()
	{
		$o = new ArrayObject(array(1, 2, 3));
		
		$this->assertSame(count($o),
			$this->e->setCountObjects(true)->editProperty($o));
	}
	
	function testEditPropertyReturnsZeroIfSpecifiedValueIsArrayAndCountArraysIsFalse()
	{
		$this->assertSame(0,
			$this->e->setCountArrays(false)->editProperty(array(1, 2, 3)));
	}
	
	function testEditPropertyReturnsSizeOfArrayIfSpecifiedValueIsArrayAndCountArraysIsTrue()
	{
		$a = array(1, 2, 3);
		
		$this->assertSame(count($a),
			$this->e->setCountArrays(true)->editProperty($a));
	}
	
	function testEditPropertyReturnsZeroIfSpecifiedValueIsNull()
	{
		$this->assertSame(0, $this->e->editProperty(null));
	}
	
	function testEditPropertyReturnsZeroIfSpecifiedValueIsResource()
	{
		$h = fopen(__FILE__, 'r');
		
		try
		{
			$this->assertSame(0, $this->e->editProperty($h));
		}
		catch (Exception $x)
		{
			fclose($h);
			
			throw $x;
		}
	}
	
	function testEditPropertyReturnsZeroIfSpecifiedValueIsBooleanFalse()
	{
		$this->assertSame(0, $this->e->editProperty(false));
	}
	
	function testEditPropertyReturnsOneIfSpecifiedValueIsBooleanTrue()
	{
		$this->assertSame(1, $this->e->editProperty(true));
	}
	
	function testEditPropertyCastsStringsToIntegers()
	{
		$this->assertSame(-1, $this->e->editProperty('-1'));
		$this->assertSame(0, $this->e->editProperty('0'));
		$this->assertSame(1, $this->e->editProperty('1'));
		$this->assertSame(1, $this->e->editProperty('1.2'));
		$this->assertSame(1, $this->e->editProperty('1aa'));
		$this->assertSame(1337, $this->e->editProperty('1337skillz'));
	}
	
	function testEditPropertyCastsFloatsToIntegers()
	{
		$this->assertSame(-1, $this->e->editProperty(-1.1));
		$this->assertSame(0, $this->e->editProperty((float)0));
		$this->assertSame(1, $this->e->editProperty(1.1));
	}
}